package by.bsu.scs.presentation.action;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import by.bsu.scs.presentation.form.LiteratureForm;
import by.bsu.scs.util.form.FileManager;
import by.bsu.scs.util.form.LiteratureFormFiller;
import by.bsu.scs.util.manager.SessionManager;
/**
 * 
 * @author Svetlana Voiteh
 * @version 1.0
 *
 * All actions connected with literature.
 */
public class LiteratureAction extends DispatchAction {

	private static final String INDEX = "index";
	private static final String LIST = "list";

	/** 
	 * Download file
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward download(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LiteratureForm literatureForm = (LiteratureForm) form;
		FileManager fileDownload = new FileManager();
		File file = literatureForm.getLiteratures()
				.get(Integer.parseInt(request.getParameter(INDEX)))
				.getLiterature();
		fileDownload.download(file, response);
		return null;
	}

	/**
	 * Create list of literature for discipline
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		LiteratureForm literatureForm = (LiteratureForm) form;
		LiteratureFormFiller literatureFormFiller = new LiteratureFormFiller(
				literatureForm, SessionManager.getInstance().getUser(request));
		literatureFormFiller.fillElements();
		return mapping.findForward(LIST);
	}
		
}
